<?php

namespace app\api\controller;

use app\services\PaymentService;
use app\services\StoreService;
use plugin\admin\app\model\Store;
use support\Request;

class StoreController
{
    public function list(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $categoryId = $request->post('category_id', 0);
            $longitude = $request->post('longitude', 116.39392);
            $latitude = $request->post('latitude', 39.919141);
            return ok_rsp('成功', StoreService::list($categoryId, $page, 10, $longitude, $latitude));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function categories(Request $request)
    {
        try {
            return ok_rsp('成功', StoreService::categories());
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function food(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $longitude = $request->post('longitude', 116.39392);
            $latitude = $request->post('latitude', 39.919141);
            return ok_rsp('成功', StoreService::list(11, $page, 10, $longitude, $latitude));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function health(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $longitude = $request->post('longitude', 116.39392);
            $latitude = $request->post('latitude', 39.919141);
            return ok_rsp('成功', StoreService::list(12, $page, 10, $longitude, $latitude));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function hotel(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $longitude = $request->post('longitude', 116.39392);
            $latitude = $request->post('latitude', 39.919141);
            return ok_rsp('成功', StoreService::list(13, $page, 10, $longitude, $latitude));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function project(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $longitude = $request->post('longitude', 116.39392);
            $latitude = $request->post('latitude', 39.919141);
            return ok_rsp('成功', StoreService::list(9, $page, 10, $longitude, $latitude));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function agent(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $longitude = $request->post('longitude', 116.39392);
            $latitude = $request->post('latitude', 39.919141);
            return ok_rsp('成功', StoreService::list(10, $page, 10, $longitude, $latitude));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function detail(Request $request)
    {
        try {
            $id = $request->post('id');
            return ok_rsp('成功', StoreService::detail($id));
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function contact(Request $request)
    {
        try {
            $id = $request->post('id');
            $name = $request->post('name');
            $mobile = $request->post('mobile');

            if (!$name || !$mobile) {
                throw new \Exception('姓名或手机号不能为空');
            }

            StoreService::contact([
                'id' => $id,
                'name' => $name,
                'mobile' => $mobile
            ]);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function apply(Request $request)
    {
        try {
            $data = $request->post();

            $validate = new \app\api\validation\Store();

            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            StoreService::apply($data);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function applyDetail(Request $request)
    {
        try {

            $data = StoreService::applyDetail();

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function liveRooms(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $data = StoreService::liveRooms($page, 10);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}